package com.ruoyi.common.utils.dd;

import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.ruoyi.common.config.DDConstant;
import com.ruoyi.common.config.RobotKey;

/**
 * @Author: Blitzcrank007
 * @CreateTime: 2023年08月23 14:40
 * 用于机器人信息发送
 */
public class RobotMessageUtil {

    /**
     * 初始化--机器人Client
     *
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dingtalkrobot_1_0.Client createClient() throws Exception {
        Config config = new Config();
        config.protocol = "https";
        config.regionId = "central";
        return new com.aliyun.dingtalkrobot_1_0.Client(config);
    }


    /**
     * 接收定时任务发送的数据，对钉钉指定用户发送提示
     * @param userId
     * @param robot
     * @throws Exception
     */

    public static void RobotMessage(String userId,Robot robot) throws Exception {

        com.aliyun.dingtalkrobot_1_0.Client client = RobotMessageUtil.createClient();
        com.aliyun.dingtalkrobot_1_0.models.BatchSendOTOHeaders batchSendOTOHeaders = new com.aliyun.dingtalkrobot_1_0.models.BatchSendOTOHeaders();
        batchSendOTOHeaders.xAcsDingtalkAccessToken = AccessTokenUtil.getAccessToken();
        com.aliyun.dingtalkrobot_1_0.models.BatchSendOTORequest batchSendOTORequest = new com.aliyun.dingtalkrobot_1_0.models.BatchSendOTORequest()
                .setMsgParam("{\"title\": \"" + robot.getTitle() + "\", \"text\": \""+ robot.getText() +"\"," +
                        "\"singleTitle\": \"查看详情\",\"singleURL\": \"http://47.100.170.171:8121/crm/order/crm/order\"}")
                .setMsgKey(RobotKey.SampleActionCard)
                .setRobotCode(DDConstant.Robot_Code)
                .setUserIds(java.util.Arrays.asList(userId));
        try {

        client.batchSendOTOWithOptions(batchSendOTORequest, batchSendOTOHeaders, new com.aliyun.teautil.models.RuntimeOptions());
        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }

        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }

        }

    }

    /**
     * 机器人实体类
     */
    public static class Robot{

        // 标题
        private String title;
        // 内容
        private String text;
        // 按钮文字
        private String singleTitle;
        // 跳转链接
        private String singleURL;

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public String getSingleTitle() {
            return singleTitle;
        }

        public void setSingleTitle(String singleTitle) {
            this.singleTitle = singleTitle;
        }

        public String getSingleURL() {
            return singleURL;
        }

        public void setSingleURL(String singleURL) {
            this.singleURL = singleURL;
        }

        @Override
        public String toString() {
            return "Robot{" +
                    "title='" + title + '\'' +
                    ", text='" + text + '\'' +
                    ", singleTitle='" + singleTitle + '\'' +
                    ", singleURL='" + singleURL + '\'' +
                    '}';
        }
    }
}
